package com.loong.member.controller.forehead;

import com.loong.common.domain.BaseQuery;
import com.loong.common.mvc.BaseForeheadController;
import com.loong.common.param.BaseIdParam;
import com.loong.common.rest.Result;
import com.loong.common.validator.group.CommonInsert;
import com.loong.common.validator.group.CommonUpdate;
import com.loong.common.validator.group.CommonValidate;
import com.loong.member.common.forehead.param.MemberAddressDetailQueryForeheadParam;
import com.loong.member.common.forehead.param.MemberAddressForeheadParam;
import com.loong.member.common.forehead.param.MemberAddressQueryForeheadParam;
import com.loong.member.handler.MemberAddressHandler;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: 处理前台会员地址相关请求的controller
 * @Author: LiuQun
 * @Date: 2020/12/25 18:09
 */
@RestController
@RequestMapping("forehead/member/memberAddress")
public class MemberAddressController extends BaseForeheadController {

    @Resource
    private MemberAddressHandler addressHandler;

    /**
     * 新增会员地址
     * @param param 会员地址参数
     * @return 响应的结果
     */
    @PostMapping("save")
    public Result saveMemberAddress(@RequestBody @Validated({CommonInsert.class}) MemberAddressForeheadParam param){
        param.setId(null);
        param.setUserId(super.getUserDTO().getId());
        return handlerExecutor.execute(() -> addressHandler.foreheadSaveMemberAddress(param));
    }

    /**
     * 更新会员地址
     * @param param 会员地址参数
     * @return 响应的结果
     */
    @PostMapping("update")
    public Result updateMemberAddress(@RequestBody @Validated({CommonUpdate.class}) MemberAddressForeheadParam param){
        return handlerExecutor.execute(() -> addressHandler.foreheadUpdateMemberAddress(param));
    }

    /**
     * 设置默认会员地址
     * @param param 会员地址id
     * @return 响应的结果
     */
    @PostMapping("update/defaultAddress")
    public Result updateDefaultMemberAddress(@RequestBody @Validated({CommonValidate.class}) BaseIdParam param){
        Long userId = super.getUserDTO().getId();
        return handlerExecutor.execute(() -> addressHandler.foreheadUpdateDefaultMemberAddress(param.getId(),userId));
    }

    /**
     * 删除会员地址
     * @param param 会员地址id
     * @return 响应的结果
     */
    @PostMapping("delete")
    public Result deleteMemberAddress(@RequestBody @Validated({CommonValidate.class})BaseIdParam param){
        return handlerExecutor.execute(() -> addressHandler.foreheadDeleteMemberAddress(param.getId()));
    }

    /**
     * 分页查询会员地址
     * @param baseQuery 分页参数
     * @return 响应的结果
     */
    @PostMapping("pageQuery/MemberAddressList")
    public Result pageQueryMemberAddress(@RequestBody BaseQuery baseQuery){
        return handlerExecutor.execute(() -> addressHandler.foreheadPageQueryMemberAddress(baseQuery,super.getUserDTO().getId()));
    }

    /**
     * 获取会员的默认地址
     * @return 响应的结果
     */
    @PostMapping("find/defaultAddress")
    public Result findDefaultMemberAddress(){
        Long userId = super.getUserDTO().getId();
        return handlerExecutor.execute(() -> addressHandler.foreheadFindDefaultMemberAddressByUserId(userId));
    }

    /**
     * 查询会员地址详情
     * @param param 会员地址id和用户id封装的参数
     * @return 响应的结果
     */
    @PostMapping("memberAddressDetail")
    public Result findMemberAddressDetail(@RequestBody @Validated({CommonValidate.class})MemberAddressDetailQueryForeheadParam param){
        param.setUserId(super.getUserDTO().getId());
        return handlerExecutor.execute(() -> addressHandler.foreheadFindMemberAddressByParam(param));
    }
}
